﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Web.UI;
using Acms.Bll;

namespace Acms.AcmsControls.ContentControls.Admin
{
    public class FlashAdminEdit : BaseAdminEdit<FlashContentControlProperties>
    {
        protected override string GetCssClass
        {
            get { return "flash"; }
        }

        public override void PropertiesLoaded(FlashContentControlProperties properties)
        {
            if (properties != null)
            {
                flash.FlashFileUrl = RenderFile.GetUrl(properties.ResourceId, Page);
                flash.Width = properties.Width > 500 ? 500 : properties.Width;
                flash.Height = properties.Height > 500 ? 500 : properties.Height;
                flash.MinVersion = properties.MinVersion;
                width.Text = properties.Width.ToString();
                height.Text = properties.Height.ToString();
                minVersion.Text = properties.MinVersion;
            }
        }

        protected override void SaveToProperties(FlashContentControlProperties properties, Acms.Bll.PostControl control)
        {
            if (upload.UploadedFiles.Count == 1)
            {
                File file = File.CreateInstance(File.FileTypes.Flash, upload.UploadedFiles[0].GetNameWithoutExtension(), upload.UploadedFiles[0].ContentType, upload.UploadedFiles[0].ContentLength, upload.UploadedFiles[0].InputStream);

                file.BeginTransaction();
                control.UseTransaction(file.TransactionKey);

                file.Update();

                properties.ResourceId = file.Id;
            }
            try
            {
                properties.Width = int.Parse(width.Text);
            }
            catch
            {
            }
            try
            {
                properties.Height = int.Parse(height.Text);
            }
            catch
            {
            }
            properties.MinVersion = minVersion.Text;
        }

        RadUpload upload = new RadUpload();
        Acms.WebControls.FlashControl flash = new Acms.WebControls.FlashControl();
        TextBox width = new TextBox();
        TextBox height = new TextBox();
        TextBox minVersion = new TextBox();

        protected override void CreateChildControls()
        {
            upload.ControlObjectsVisibility = ControlObjectsVisibility.None;
            this.Controls.Add(upload);

            this.Controls.Add(minVersion);
            this.Controls.Add(width);
            this.Controls.Add(height);

            flash.WMode = Acms.WebControls.FlashControl.WModes.Transparent;
            this.Controls.Add(flash);

            base.CreateChildControls();
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            upload.RenderControl(writer);

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "form");
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);

            writer.RenderBeginTag(HtmlTextWriterTag.Li);

            writer.AddAttribute(HtmlTextWriterAttribute.For, width.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write("Width:");
            writer.RenderEndTag();//lable

            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            width.RenderControl(writer);
            writer.RenderEndTag();//span

            writer.RenderEndTag();//li

            writer.RenderBeginTag(HtmlTextWriterTag.Li);

            writer.AddAttribute(HtmlTextWriterAttribute.For, height.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write("Height:");
            writer.RenderEndTag();//lable

            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            height.RenderControl(writer);
            writer.RenderEndTag();//span

            writer.RenderEndTag();//li

            writer.RenderBeginTag(HtmlTextWriterTag.Li);

            writer.AddAttribute(HtmlTextWriterAttribute.For, minVersion.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write("Minium Flash Version:");
            writer.RenderEndTag();//lable

            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            minVersion.RenderControl(writer);
            writer.RenderEndTag();//span

            writer.RenderEndTag();//li
            
            writer.RenderEndTag();//ul

            if (!string.IsNullOrEmpty(flash.FlashFileUrl))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "form");
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);

                writer.RenderBeginTag(HtmlTextWriterTag.Li);
                writer.AddAttribute(HtmlTextWriterAttribute.For, flash.ClientID);
                writer.RenderBeginTag(HtmlTextWriterTag.Label);
                writer.Write("Existing Flash File:");
                writer.RenderEndTag();//lable

                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                flash.RenderControl(writer);
                writer.RenderEndTag();//span

                writer.RenderEndTag();//li


                writer.RenderEndTag();//ul
            }
        }
    }
}
